// 主界面
;define(function(require, exports, module) {
    'use strict';
    var util = require("util");
    var base = require("base");
    var router = require("../hash/pageRouter");
    var nature = $("#nature").val();
	var enterpriseId;           
	var validate = require("validate");
	var tools = require("tools");
	var databind = require("databind");
	var modifyClienManage_details = require("./modifyClienManage_details.css");
	var authenticationId;
    var init = function(page){
    	authenticationId = page.params.authenticationId;
		$(".nav_active").removeClass("nav_active1");
		$(".authenticationState").addClass("nav_active1");
		inquireEnterpriseDetail(enterpriseId);
		router.init(); 
		initEvent();
    };
    
	var initEvent = function(){		
		$("#modifyClienManage_details").on("change", "#creditCard", function(){
			var bn = tools.getBankName($(this).val());
			if (bn == ""){
				alert("未知开户行！");
			} else {
				$("#depositBank").val(bn);
			}
		});
		
		$("#modifyClienManage_details").on("change", "#legalIdcard", function(){
			var promptString = validate.isIdCard($(this), "请输入正确的身份证号码!");
			if ("" != promptString) {
				base.alert(2, promptString)
			} 
		});
		
		layer.tips(tools.formatCurrency($("input[name=registeredCapital]").val()) + '(' + tools.changeMoneyToChinese($("input[name=registeredCapital]").val()) + ')', 'input[name=registeredCapital]', {
		  tips: [1, '#3595CC'],
		  time: 3000
		});
		
		$("#modifyClienManage_details").on("focus", "input[name=registeredCapital]", function(){
			var $this = $(this);
			layer.tips(tools.formatCurrency($this.val()) + '(' + tools.changeMoneyToChinese($("input[name=registeredCapital]").val()) + ')', 'input[name=registeredCapital]', {
			  tips: [1, '#3595CC'],
			  time: 3000
			});
		});
		
		$("#modifyClienManage_details").on("change", "input[name=registeredCapital]", function(){
			var $this = $(this);
			layer.tips(tools.formatCurrency($this.val()) + '(' + tools.changeMoneyToChinese($("input[name=registeredCapital]").val()) + ')', 'input[name=registeredCapital]', {
			  tips: [1, '#3595CC'],
			  time: 3000
			});
		});
		
		//认证提交
		$("#modifyClienManage_details").on("click", ".next", function(){
			databind.submitDom($("#modifyClienManage_details"), function(){//获取div的id值通过HTML中的action="informationCheck"传到后台
				history.go(-1);
		  	});
		});
		
//		单位类型 nature 
//		企业名 enterpriseName 
//		单位注册号 businessLicence 
//		营业执照所在地 address
//		联系地址 contactAddress
//		经营范围 businessScope
//		注册资金 registeredCapital
//		营业期限 businessTimeLimit
//		法定代表人姓名 legalPerson
//		身份证号 legalIdcard
//		证件有效期 legalPeriod
//		开户行 depositBank
//		开户名称 accountHolder
//		银行卡号 creditCard

		//点击企业基本信息
		$(".modifyClienManage_details1 .enterpriseInfo").on("click", function() {
			router.route("client/modifyClienManage_details.html?authenticationId=" + authenticationId, true);
			history.go(0);
		});

		//证明材料
		$(".financeEvidentiary").on("click", function() {
			router.route("client/clientManage_modifyEvidentiary.html?authenticationId=" + authenticationId);
		});
		
		//审核通过
		$("#modifyClienManage_details .button-Save").unbind("click").click(function(){
			base.fnAjax({
				action: "modifyCertificationAudit.json",
				oData: {
					authenticationId: authenticationId
				}, //传递参数
				fnSuccess: function(data) {
					 if (data.status == 200) {
						 	base.alert(1, "提交成功！");
		                	location.href="../client/ModifyclientManage.html";
		                } else {
		                }
				}
			});
		});
		
		//审核拒绝
		$("#modifyClienManage_details .button-Submit").unbind("click").click(function(){
			base.fnAjax({
				action: "auditCertification.json",
				oData: {
					authenticationId: authenticationId
				}, //传递参数
				fnSuccess: function(data) {
					 if (data.status == 200) {
		                	base.alert(1, "提交成功！");
		                	location.href="../client/ModifyclientManage.html";
		                } else {
		                }
				}
			});
		});

    };
    
	
	
    //查询企业详细信息
	var inquireEnterpriseDetail = function() {
		base.fnAjax({
			action: "queryEnterpriseModify.json",//queryEnterpriseModify
			oData: {
				authenticationId: authenticationId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					databind.fillForm($("#modifyClienManage_details"), data.datas[0]);
					$("select[name=province]").html("<option>"+data.datas[0].province+"</option>");
					$("select[name=city]").html("<option>"+data.datas[0].city+"</option>");
			        $("select[name=district]").html("<option>"+data.datas[0].district+"</option>");
				} else {
					base.alert(2, data.message);
				}
				//判断认证状态
					if(data.datas[0].authentication !=undefined && data.datas[0].authentication != 3) { //未认证
						$(".return").hide();
					}
					if(data.datas[0].authentication !=undefined && data.datas[0].authentication == 3) { //已认证
						$(".next").hide();
					}
				//调显示与隐藏函数
				hideSpan();
		        hideCardType();
			}
		});
		databind.disableInput("#content");
		databind.enableInput("#button-submit")
	};

		//联动1
	var hideSpan = function() {
		var sUserType = $("#sUserType").val();
		if(sUserType == "以代理人身份注册") {
			$("#hide1").show();
			$("#hide2").show();
			$("#procurator").attr('isNull',1);
			$("#procuratorIdcard").attr('isNull',1);
			$("#procuratorPeriod").attr('isNull',1);
		}
		if(sUserType == "以法定代表人身份注册") {
			$("#hide2").hide();
			$("#hide1").show();
			$("#procurator").attr('isNull',0);
			$("#procuratorIdcard").attr('isNull',0);
			$("#procuratorPeriod").attr('isNull',0);
		}
	};
	
	//联动2
	var hideCardType = function() {
		var scardType = $("#scardType").val();
		if(scardType == "0") {
			$("#hideRegNo").show();
			$("#certificateShow").show();
			$("#codeUSCHide").hide();
			$("#codeUSC").attr('isNull',0);
			$("#regNo").attr('isNull',1);
			$("#certificate").attr('isNull',1);
		}
		if(scardType == "1") {
			$("#hideRegNo").hide();
			$("#certificateShow").hide();
			$("#codeUSCHide").show();
			$("#codeUSC").attr('isNull',1);
			$("#regNo").attr('isNull',0);
			$("#certificate").attr('isNull',0);
		}
	};
	
	//连接e签宝生成证书,返回账户唯一标识
    var addOrganize = function(){
		base.fnAjax({
			action: "addOrganize.json",
			async: false,
			oData: {
            }, //传递参数
            fnSuccess: function(data) {
            }
		});
	};
	
    return {
        init:init
    };
});